package com.ellwood.controller;

import com.ellwood.api.system.iController.IUserController;
import com.ellwood.common.core.base.ControllerFactory;
import com.ellwood.common.core.enums.HttpCodeEnum;
import com.ellwood.common.core.result.ApiResult;
import com.ellwood.common.core.result.PageResult;
import com.ellwood.dataMapper.system.entity.UserEntity;
import com.ellwood.modules.system.domain.VO.user.UserEditVO;
import com.ellwood.modules.system.domain.VO.user.UserPageQueryVO;
import com.ellwood.modules.system.domain.VO.user.UserQueryVo;
import com.ellwood.modules.system.domain.VO.user.UserSaveVo;
import com.ellwood.modules.system.domain.dto.UserPageQueryDto;
import com.ellwood.modules.system.domain.dto.UserQueryDto;
import com.ellwood.modules.system.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ProjectName: Ellwood-framework-boot
 * @PackageName: com.ellwood.controller
 * @ClassName: UserController
 * @Description: // 用户控制器
 * @Author: Ellwood
 * @CreateDate: 2024/7/22 - 上午11:29
 * @Version: V1.0.0
 */

@Validated
@AllArgsConstructor
@Tag(name = "用户管理", description = "用户管理")
@RestController
@RequestMapping("/api/sys/user/")
public class UserController extends ControllerFactory<UserEntity> implements IUserController {

    private UserService userService;

    @Operation(summary = "分页查询用户")
    @GetMapping("page")
    @Override
    public ApiResult<PageResult<UserPageQueryDto>> pageQuery(@ParameterObject @Valid UserPageQueryVO pageVo) {
        PageResult<UserPageQueryDto> pageResult = userService.pageQuery(pageVo);
        return ApiResult.okResult(pageResult);
    }

    @Operation(summary = "查询所有用户")
    @GetMapping("findAll")
    @Override
    public ApiResult<List<UserQueryDto>> findAll() {
        List<UserQueryDto> dataList = userService.findAll(new UserQueryVo());
        return ApiResult.okResult(dataList);
    }

    @Operation(summary = "根据ID查询用户")
    @GetMapping("{id}")
    @Override
    public ApiResult<UserQueryDto> findById(@PathVariable("id") Long id) {
        UserQueryDto userQueryDto = userService.findById(id);
        return ApiResult.okResult(userQueryDto);
    }

    @Operation(summary = "保存用户")
    @PostMapping
    @Override
    public ApiResult<String> saveUser(@RequestBody @Valid UserSaveVo userVo) {
        userService.saveUser(userVo);
        return ApiResult.Success(HttpCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新用户")
    @PutMapping("/updateUser")
    @Override
    public ApiResult<String> updateUser(@RequestBody @Valid UserEditVO userVo) {
        userService.updateUser(userVo);
        return ApiResult.Success(HttpCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("{id}")
    @Override
    public ApiResult<String> delete(@PathVariable("id") Long id) {
        boolean result = userService.removeById(id);
        return result ? ApiResult.Success(HttpCodeEnum.SUCCESS) : ApiResult.errorResult(HttpCodeEnum.ERROR);
    }

    @DeleteMapping("removeByIds")
    @Operation(summary = "批量删除用户")
    @Override
    public ApiResult<String> removeByIds(@RequestBody List<Long> idList) {
        // 判断要删除的是否为当前登录用户/超级管理人员，这个后面实现，需要登录认证权限 TODO

        boolean result = !userService.delete(idList);
        return result ? ApiResult.Success(HttpCodeEnum.SUCCESS) : ApiResult.errorResult(HttpCodeEnum.ERROR);
    }
}

